home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / glx_extensions / swap_control.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.9 KB  |  275 lines

  1. /*
  2.  * Copyright 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /* swap_control.c - demonstrate swap_control extension
  18.  * 
  19.  *    Up Arrow     - increment swap interval
  20.  *    Down Arrow     - decrement swap interval
  21.  *    Escape key    - exit program
  22.  */
  23. /* compile: cc -o swap_control swap_control.c -lGLU -lGL -lX11 */
  24.  
  25. #include <GL/glx.h>
  26. #include <GL/glu.h>
  27. #include <X11/keysym.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <math.h>
  31.  
  32. /*  Function Prototypes  */
  33.  
  34. GLvoid  initgfx( GLvoid );
  35. GLvoid  drawScene( GLvoid );
  36. GLvoid    incInterval( GLvoid );
  37. GLvoid    decInterval( GLvoid );
  38.  
  39. static void printHelp( char * );
  40. static void renderBitmapString( GLuint, char * );
  41. static GLuint LoadFont(Display *dpy, char *fontName);
  42.  
  43. static char *fontName = "-*-helvetica-*-r-normal-*-14-*-*-*-*-*-*-*";
  44. static GLuint font;
  45.  
  46. static int interval = 1;
  47. static GLfloat angle = 0.0;
  48.  
  49. static Bool
  50. process_input( Display *dpy ) 
  51. {
  52.     XEvent event;
  53.     static GLboolean animate = GL_TRUE;
  54.     Bool redraw = 0;
  55.     GLsizei w, h;
  56.  
  57.     while (XPending(dpy)) {
  58.         char buf[31];
  59.         KeySym keysym;
  60.  
  61.         XNextEvent(dpy, &event);
  62.         switch(event.type) {
  63.         case Expose:
  64.             redraw = 1;
  65.             break;
  66.         case ConfigureNotify:
  67.             w = event.xconfigure.width;
  68.             h = event.xconfigure.height;
  69.             glViewport(0, 0, w, h);
  70.             redraw = 1;
  71.             break;
  72.         case MapNotify:
  73.             animate = GL_TRUE; 
  74.             break;
  75.         case UnmapNotify:
  76.             animate = GL_FALSE; 
  77.             break;
  78.         case KeyPress:
  79.             (void) XLookupString(&event.xkey, buf, sizeof(buf), 
  80.                         &keysym, NULL);
  81.             switch (keysym) {
  82.             case XK_Up:    /* increment */
  83.                 incInterval();
  84.                 break;
  85.             case XK_Down:
  86.                 decInterval();
  87.                 break;
  88.             case XK_Escape:
  89.                 exit(EXIT_SUCCESS);
  90.                 break;
  91.             default:
  92.                 break;
  93.             }
  94.         default:
  95.             break;
  96.         }
  97.     }
  98.     return redraw || animate;
  99. }
  100.  
  101. int
  102. main(int argc, char **argv) 
  103. {
  104.     int attributeList[] = { GLX_DOUBLEBUFFER, 
  105.                 GLX_RGBA, GLX_RED_SIZE, 1, None };
  106.     Display     *dpy;
  107.     XVisualInfo     *vi;
  108.     GLXContext     cx;
  109.     Bool         isdirect;
  110.     XSetWindowAttributes swa;
  111.     Window         win;
  112.     const char    *s = NULL;
  113.     int         major, minor;
  114.  
  115.  
  116.     dpy = XOpenDisplay(0);
  117.  
  118.     if (!(vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList))) {
  119.         fprintf(stderr, "overlay: no suitable RGB visual available\n");
  120.         exit(EXIT_FAILURE);
  121.     }
  122.  
  123.     /* create a GLX context */
  124.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  125.     isdirect = glXIsDirect(dpy, cx);
  126.  
  127.     swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
  128.         vi->visual, AllocNone);
  129.  
  130.     swa.border_pixel = 0;
  131.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask;
  132.     win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 500, 500,
  133.         0, vi->depth, InputOutput, vi->visual,
  134.         CWBorderPixel|CWColormap|CWEventMask, &swa);
  135.     XStoreName(dpy, win, argv[0]);
  136.     XMapWindow(dpy, win);
  137.  
  138.     glXMakeCurrent(dpy, win, cx);
  139.  
  140.     font = LoadFont( dpy, fontName );
  141.  
  142. #ifdef GLX_SGI_swap_control
  143.     if ( glXQueryVersion(dpy, &major, &minor) == GL_FALSE ) {
  144.         fprintf(stderr, "glXQueryVersion() failed.\n");
  145.         exit(-1);
  146.     }
  147. #ifdef GLX_VERSION_1_1
  148.     if ( minor > 0 || major > 1 )
  149.         s = glXQueryExtensionsString( dpy, DefaultScreen(dpy) );
  150. #endif
  151. #endif
  152.     if (!s || !strstr(s, "GLX_SGI_swap_control")) {
  153.         fprintf(stderr, "GLX_SGI_swap_control not supported\n");
  154.     }
  155.  
  156.     initgfx();
  157.  
  158.     printHelp( argv[0] );
  159.  
  160.     while (1) {
  161.         if (process_input(dpy)) {
  162.             drawScene();
  163.             glXSwapBuffers(dpy, win);
  164.             if (!isdirect) glFinish();
  165.         }
  166.     }
  167. }
  168.  
  169. GLvoid
  170. printHelp( char *progname )
  171. {
  172.     fprintf(stdout, "\n%s - demonstrates swap_control extension\n"
  173.         "Escape key    - exit the program\n"
  174.         "Up Arrow     - increment swap interval\n" 
  175.         "Down Arrow     - decrement swap interval\n\n", 
  176.         progname);
  177. }
  178.  
  179. static GLuint 
  180. LoadFont(Display *dpy, char *fontName)
  181. {
  182.     XFontStruct *fontInfo;
  183.     unsigned long first, last;
  184.     GLuint fontBase;
  185.  
  186.     fontInfo = XLoadQueryFont(dpy, fontName);
  187.     if (fontInfo == NULL) {
  188.         fprintf(stderr, "Can't find font \"%s\"\n", fontName);
  189.         return;
  190.     }
  191.  
  192.     first = fontInfo->min_char_or_byte2;
  193.     last = fontInfo->max_char_or_byte2;
  194.  
  195.     fontBase = glGenLists(last+1);
  196.     if (fontBase == 0) {
  197.         fprintf(stderr, "Out of list space\n");
  198.         return;
  199.     }
  200.     glXUseXFont(fontInfo->fid, first, last-first+1, fontBase+first);
  201.  
  202.     return( fontBase );
  203. }
  204.  
  205. GLvoid
  206. renderBitmapString( GLuint fontBase, char *string )
  207. {
  208.     glPushAttrib( GL_LIST_BIT );
  209.         glListBase( fontBase );
  210.         glCallLists( strlen(string), GL_UNSIGNED_BYTE, string );
  211.     glPopAttrib();
  212. }
  213.         
  214.  
  215. GLvoid
  216. initgfx( GLvoid )
  217. {
  218.     glClearColor( 0.5, 0.5, 0.5, 1.0 );
  219.  
  220. #ifdef GLX_SGI_swap_control
  221.     glXSwapIntervalSGI( interval );
  222. #endif
  223.     glMatrixMode( GL_PROJECTION );
  224.     glLoadIdentity();
  225.     glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
  226.     glMatrixMode( GL_MODELVIEW );
  227.     glLoadIdentity();
  228. }
  229.  
  230. GLvoid
  231. incInterval( GLvoid )
  232. {
  233.     interval++;
  234. #ifdef GLX_SGI_swap_control
  235.     glXSwapIntervalSGI( interval );
  236. #endif
  237. }
  238.  
  239. GLvoid
  240. decInterval( GLvoid )
  241. {
  242.     if ( interval > 1 ) {
  243.         interval--;
  244. #ifdef GLX_SGI_swap_control
  245.         glXSwapIntervalSGI( interval );
  246. #endif
  247.     }
  248. }
  249.  
  250. GLvoid
  251. drawScene( GLvoid )
  252. {
  253.     char buf[20];
  254.  
  255.     glClear( GL_COLOR_BUFFER_BIT );
  256.  
  257.     glPushMatrix();
  258.         glRotatef( angle, 0.0, 0.0, -1.0 );
  259.         glColor3f( 1.0, 1.0, 0.0 );
  260.         glBegin( GL_TRIANGLES );
  261.             glVertex2f( -0.1, -0.1 );
  262.             glVertex2f(  0.1, -0.1 );
  263.             glVertex2f(  0.0,  0.9 );
  264.         glEnd();
  265.     glPopMatrix();
  266.  
  267.     /* print stat info */
  268.     glColor3f( 0.0, 0.0, 0.0 );
  269.     sprintf( buf, "swap interval: %3d", interval );
  270.     glRasterPos2f( -0.5, -0.5 );
  271.     renderBitmapString( font, buf );
  272.  
  273.     angle = fmodf( (angle + 6.0), 360.0 );
  274. }
  275.